<template>
  <div class="permission-container">
    <div class="box">
      <div>
        <el-button type="primary" size="mini" @click="addPermissionTmp(1,0)">添加权限</el-button>
      </div>
      <div class="table">
        <el-table
          :data="tableData"
          style="width: 100%"
          row-key="id"
          :row-class-name="tableRowClassName"
        >
          <el-table-column
            prop="name"
            label="名称"
          />
          <el-table-column
            prop="code"
            label="标识"
          />
          <el-table-column
            prop="description"
            label="描述"
          />
          <el-table-column
            label="操作"
            align="center"
          >
            <template v-slot="{row}">
              <!-- ?????????????? -->
              <el-button v-if="row.type === 1" type="text" size="small" @click="addPermissionTmp(2,row.id)">添加</el-button>
              <el-button type="text" size="small" @click="editPermissionTmp(row.id)">编辑</el-button>
              <el-button slot="reference" style="margin-left: 10px;" type="text" size="small" @click="delPermission(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-dialog :title="isEdit ? '编辑权限':'新增权限'" :visible.sync="showDialog" @close="close">
        <el-form ref="formDateRef" :model="formDate" :rules="formDateRules">
          <el-form-item label="权限名称" label-width="120px" prop="name">
            <el-input v-model="formDate.name" />
          </el-form-item>
          <el-form-item label="权限标识" label-width="120px" prop="code">
            <el-input v-model="formDate.code" />
          </el-form-item>
          <el-form-item label="权限描述：" label-width="120px" prop="description">
            <el-input v-model="formDate.description" />
          </el-form-item>
          <el-form-item label="开启：" label-width="120px">
            <el-switch
              v-model="formDate.enVisible"
              active-color="#13ce66"
              :active-value="1"
              :inactive-value="0"
            />
          </el-form-item>
          <el-form-item label-width="400px">
            <el-button :loading="roleLoging" type="primary" size="mini" @click="doAdd">确认</el-button>
            <el-button size="mini" @click="close">取 消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { sysPermissionListRequest,
  addPermissionRequest,
  deletePermissionRequest,
  getPermissionDetailRequest,
  updatePermissionRequest } from '@/api/permission.js'
import { transformListToTree } from '@/utils/index.js'
export default {
  name: 'Permission',
  data() {
    return {
      showDialog: false,
      tableData: [],
      formDate: {
        name: '',
        code: '',
        description: '',
        enVisible: 1,
        type: null,
        pid: null
      },
      formDateRules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '权限描述不能为空', trigger: 'blur' }
        ]
      },
      isEdit: false,
      roleLoging: false
    }
  },
  created() {
    this.sysPermissionList()
  },
  methods: {
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2) {
        return 'success-row'
      }
    },
    async sysPermissionList() {
      const res = await sysPermissionListRequest()
      console.log(res)
      this.tableData = transformListToTree(res.data, 0)
    },
    // 一个按钮控制两个新增，根据后端需要的pid和type来判断是哪个新增
    addPermissionTmp(type, id) {
      this.isEdit = false
      this.showDialog = true
      // type表示是页面权限，id是按钮的权限
      this.formDate.type = type
      this.formDate.pid = id
    },
    // 删除功能
    delPermission(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deletePermissionRequest(id)
        this.sysPermissionList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    close() {
      this.isEdit = false
      this.showDialog = false
      this.$refs.formDateRef.resetFields()
      // 将编辑之后的form数据清空为原来的状态
      this.formDate = this.$options.data().formDate
    },
    doAdd() {
      this.$refs.formDateRef.validate(async(valid) => {
        if (valid) {
          // 添加加载按钮，反正连点
          this.roleLoging = true
          if (this.isEdit) {
            await updatePermissionRequest(this.formDate)
            this.$message.success('更新权限成功！')
          } else {
            await addPermissionRequest(this.formDate)
            this.$message.success('添加权限成功！')
          }
          this.roleLoging = false
          this.close()
          this.sysPermissionList()
        }
      })
    },
    // 编辑功能的数据回显
    async editPermissionTmp(id) {
      this.isEdit = true
      this.showDialog = true
      const res = await getPermissionDetailRequest(id)
      this.formDate = res.data
    }
  }
}
</script>

<style>
  .el-table .warning-row {
    background: oldlace;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }
</style>

<style lang="scss" scoped>
.permission-container{
  padding: 20px;
  min-height: calc(100vh - 80px);
  background-color: #f5f6f8;
  .box{
    background-color: #fff;
    padding: 10px;

  }
}

</style>
